package edu.sctu.yueding.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import edu.sctu.yueding.R;
import edu.sctu.yueding.fragment.myorder.MyOrderFragment;
import edu.sctu.yueding.view.CustomViewPage;

/**
 * Created by zcy on 2016/9/21.
 */
public class MyOrderListActivity extends BaseActivity implements View.OnClickListener{

    private CustomViewPage customViewPager;

    private LinearLayout waitPayL, payedL;

    private TextView waitPayText, waitPayLine, payedText, payedLine;
    private ImageView back;

    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private MyOrderFragment waitPayFragment;
    private MyOrderFragment payedFragment;
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        initSystemBar(this, 0);
        setView();
        setEven();
        util();


    }

    private void setView() {

        activity = this;

        waitPayL = (LinearLayout) findViewById(R.id.wait_pay_l);
        payedL = (LinearLayout) findViewById(R.id.pay_l);

        waitPayText = (TextView) findViewById(R.id.wait_pay_text);
        waitPayLine = (TextView) findViewById(R.id.wait_pay_line);
        payedText = (TextView) findViewById(R.id.pay_text);
        payedLine = (TextView) findViewById(R.id.pay_line);


        back = (ImageView) findViewById(R.id.my_order_back);
        customViewPager = (CustomViewPage) findViewById(R.id.my_order_viewpager);

        waitPayFragment = new MyOrderFragment();
        payedFragment = new MyOrderFragment();

    }

    private void setEven() {

        waitPayL.setOnClickListener(this);
        payedL.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    private void util() {


        Bundle bundle = new Bundle();
        bundle.putInt("flag",0);
        waitPayFragment.setArguments(bundle);

        Bundle bundle1 = new Bundle();
        bundle.putInt("flag",1);
        payedFragment.setArguments(bundle1);

        fragmentList.add(waitPayFragment);
        fragmentList.add(payedFragment);

        customViewPager.setAdapter(new MyFrageStatePagerAdapter(getSupportFragmentManager()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wait_pay_l:
                changeColor(waitPayText,waitPayLine,0);
                customViewPager.setCurrentItem(0);
                break;
            case R.id.pay_l:
                changeColor(payedText,payedLine,1);
                customViewPager.setCurrentItem(1);
                break;
            case R.id.my_order_back:
                finish();
                break;
        }
    }

    private void changeColor(TextView text,TextView line,int flag) {

        if (flag == 0){
            payedText.setTextColor(getResources().getColor(R.color.black));
            payedLine.setBackgroundColor(getResources().getColor(R.color.white));

        }else {

            waitPayText.setTextColor(getResources().getColor(R.color.black));
            waitPayLine.setBackgroundColor(getResources().getColor(R.color.white));
        }
        text.setTextColor(getResources().getColor(R.color.main_color));
        line.setBackgroundColor(getResources().getColor(R.color.main_color));
    }

    private class MyFrageStatePagerAdapter extends FragmentStatePagerAdapter {

        public MyFrageStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {

            return fragmentList.get(i);
        }
        @Override
        public int getCount() {
            return fragmentList.size();

        }
        @Override
        public void finishUpdate(ViewGroup container) {
            super.finishUpdate(container);
            switch (customViewPager.getCurrentItem()) {
                case 0:
                    changeColor(waitPayText,waitPayLine,0);
                    break;
                case 1:
                    changeColor(payedText,payedLine,1);
                    break;

            }

        }

    }
}
